<?php
/**
 * This class stores jabber address book item data such as user, subscription state and connection state
 */
class Zend_Jabber_AddressBook_Item {
    /**
     * This means that both users are allowed to see each other presence information
     */
    const SUBSCRIPTION_BOTH = 'both';
    
    /**
     * This means that your current user is allowed to see distant user's presence contrariwise
     */
	const SUBSCRIPTION_TO = 'to';
    
    /**
     * This means that distant user is allowed to see your presence, but you are not allowed to see his
     */
	const SUBSCRIPTION_FROM = 'from';
	
	/**
	 * This means that both users are not allowed to see each others information
	 */
    const SUBSCRIPTION_NONE = 'none';
    
    /**
     * This subscription is used when removing user from addressBook
     */
    const SUBSCRIPTION_REMOVE = 'remove';
    
    /**
     * Owner address book
     *
     * @var Zend_Jabber_AddressBook
     */
    private $addressBook;
    
    /**
     * Distant user
     *
     * @var Zend_Jabber_User
     */
    private $user;
    
    /**
     * Array of groups (Zend_Jabber_AddressBook_Group)
     *
     * @var array
     */
    private $groups = array();
    
    /**
     * Direction of the subscription
     * 
     * There are four major values for this parameter
     *     both  - both users are subscribet to view their presence update
     *     to       - only address book owner can see other user presence
     *     from  - only distant useer can see address book's owner's presence
     *     none  - none of two can see other's presence state 
     * 
     * @var string
     */
    private $subscription;
    
    /**
     * Create new addressBook item
     *
     * @param Zend_Jabber_User $you Your user object
     * @param Zend_Jabber_User $user Your user user object
     * @param string $subscription Direction of the subscription
     * @param array $groups array of Zend_Jabber_AddressBook_Group
     */
    public function __construct(Zend_Jabber_AddressBook $addressBook, Zend_Jabber_User $user, $subscription, $groups = array()) {
        $this->addressBook = $addressBook;
        $this->user = $user;
        $this->subscription = $subscription;
        
        if(!is_array($groups)) throw new Zend_Jabber_Exception(Zend_Jabber_Exception::MESSAGE_SHOULD_BE_ARRAY_OF_ROSTER_GROPS, Zend_Jabber_Exception::CODE_SHOULD_BE_ARRAY_OF_ROSTER_GROPS);
        foreach($groups as $group) {
             if(!($group instanceof Zend_Jabber_AddressBook_Group)) throw new Zend_Jabber_Exception(Zend_Jabber_Exception::MESSAGE_SHOULD_BE_ARRAY_OF_ROSTER_GROPS, Zend_Jabber_Exception::CODE_SHOULD_BE_ARRAY_OF_ROSTER_GROPS);
             
             $this->groups[] = $group;
        }
    }
    
    /**
     * Get user subscription direction
     *
     * @return string
     */
    public function getSubscription() {
        return $this->subscription;
    }
    
    /**
     * Get item groups
     *
     * @return array
     */
    public function getGroups() {
        return $this->groups;
    }
    
    /**
     * Get distant user
     *
     * @return Zend_Jabber_User
     */
    public function getUser() {
        return $this->user;
    }
    
    /**
     * Get owner address book
     *
     * @return Zend_Jabber_AddressBook
     */
    public function getAddressBook() {
        return $this->addressBook;
    }

    /**
     * Convert address book item to DomDocument
     * 
     * @return DomDocument
     */
    public function toDom() {
        $dom = new DomDocument();
        $item = $dom->createElement('item');
        $item->setAttribute('jid', $this->getUser()->getJid(false));
        $item->setAttribute('name', $this->getUser()->getAlias());
        $item->setAttribute('subscription', $this->getSubscription());
        $dom->appendChild($item);
        
        foreach($this->getGroups() as $group) {
	        $groupNode = $dom->importNode($group->toDom()->firstChild, true);
	        $item->appendChild($groupNode);
        }
        
        return $dom;
    }
}

?>